package com.mall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.entity.ShoppingCart;
import com.mall.mapper.ShoppingCartMapper;
import com.mall.service.IShoppingCartService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @since 2024-01-15
 * 购物车服务实现类
 */
@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
    private final String GOODS_ID = "goods_id";
    private final String SHOPPING_CART_ID = "shopping_cart_id";
    private final String USER_ID = "user_id";

    final
    ShoppingCartMapper shoppingCartMapper;

    public ShoppingCartServiceImpl(ShoppingCartMapper shoppingCartMapper) {
        this.shoppingCartMapper = shoppingCartMapper;
    }

    /**
     * 查询购物车
     * @return cart_items
     */
    @Override
    public List<ShoppingCart> queryShoppingCart(String userId) {
        return shoppingCartMapper.queryShoppingCart(userId);
    }

    /**
     * 删除购物车里面的项目
     * @param ids ids
     * @return success count
     */
    @Override
    public Integer deleteShoppingCart(List<String> ids) {
        return shoppingCartMapper.deleteBatchIds(ids);
    }

    /**
     * 修改购物车项目
     * @param shoppingCartItem item_entity
     * @return bool
     */
    @Override
    public Boolean updateShoppingCart(ShoppingCart shoppingCartItem) {
        int res = shoppingCartMapper.update(shoppingCartItem,new QueryWrapper<ShoppingCart>()
                .eq(SHOPPING_CART_ID,shoppingCartItem.getShoppingCartId())
                .eq(USER_ID,shoppingCartItem.getUserId()));
        return res > 0;
    }

    /**
     * 购物车添加项目
     * @param shoppingCartItem item_entity
     * @return bool
     */
    @Override
    public Boolean addShoppingCart(ShoppingCart shoppingCartItem) {
        return shoppingCartMapper.insert(shoppingCartItem) > 0;
    }

    /**
     * 判断购物某个商品是否存在
     * @param goodsId 商品ID
     * @return bool
     */
    @Override
    public Boolean shoppingCartItemIsExist(String goodsId,String userId) {
        return shoppingCartMapper.selectCount(new QueryWrapper<ShoppingCart>()
                .eq(GOODS_ID,goodsId)
                .eq(USER_ID,userId)) > 0;
    }

    /**
     * 添加购物车已经存在的商品的购买数量
     * @param count 数量
     * @return bool
     */
    @Override
    public Boolean addShoppingCartItemCount(String goodsId,Integer count,String userId) {
        return shoppingCartMapper.addShoppingCartItemCount(goodsId,count,userId);
    }

    /**
     * 根据购物车id查询购物车信息
     * @param userId
     * @param shoppingCartIds
     * @return
     */
    @Override
    public List<ShoppingCart> queryShoppingCartByCartIds(String userId, List<Integer> shoppingCartIds) {
        return shoppingCartMapper.queryShoppingCartByCartIds(userId, shoppingCartIds);
    }
}
